package com.community.contents.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.community.common.core.constant.RedisKey;
import com.community.common.redis.utils.RedisUtils;
import com.community.contents.controller.TopicController;
import com.community.contents.entity.TopicEntity;
import com.community.contents.service.impl.TopicServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @className: HotTopicJob
 * @author: shuishui
 * @date: 2023/03/02 19:43
 */
@Component
@Slf4j
public class HotTopicJob {
    @Autowired
    private TopicServiceImpl topicService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0/15 * * * * * ")
    public void HostTopic(){


        Map mapScope = new TreeMap<Double,List<TopicEntity>>(new Comparator<Double>() {
            @Override
            public int compare(Double o1, Double o2) {
                return o2.compareTo(o1);
            }
        });
        List<TopicEntity> list1 = topicService.list();
        for (TopicEntity i:list1) {
            Double scope = i.getPostNum()*0.3 + i.getUserNum()*0.7;
            mapScope.put(scope,i);
        }

        Set<Double> s1 = mapScope.keySet();
        List topic10 = new ArrayList();
        int i = 0;
        for (Double s:s1) {
            if (i <10) {
                topic10.add(mapScope.get(s));
            }
            i++;
        }


        redisTemplate.opsForValue().set(RedisKey.HOT_TOPIC,topic10);
    }

}
